package com.excel.read;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.excel.comp.HTMLTable;
/**
 * 维护所有配置信息，包括表单，列表，数据库元数据信息等等
 * 可能以后需要改为动态的配置，而不需要加一种类，加一个类
 * @author Administrator
 *
 */
public class AllTable implements Serializable{

	/**
	 * 数据源表信息(DBTable)
	 */
    private HashMap<String, DBTable> tableMap = new HashMap<String, DBTable>();
    private List<GridTable> list;
    
    /**
     * 表单
     * 
     */
    private HashMap<String, FormTable> formMap = new HashMap<String, FormTable>();
    
    /**
     * 数据
     * 
     */
    private HashMap<String, TableDataTable> dataMap = new HashMap<String, TableDataTable>();
    
    /**
	 * 存放列表的地方
	 * 
	 */
	public static HashMap<String, GridSearchTable> gridMap = new HashMap<String, GridSearchTable>();
	
	/**
	 * 存放SQL的地方
	 * 
	 */
	public static HashMap<String, SQLTable> sqlMap = new HashMap<String, SQLTable>();
	
	
	/**
	 * 存放原始数据的地方
	 * 
	 */
	public static HashMap<String, HashMap<String, ExcelTable>> excelMap = new HashMap<String, HashMap<String, ExcelTable>>();
	
	
	
	/**
	 *数据列名字典 map<tablename.columnname, [name1, name2..]> 
	 */
	public static HashMap<String, List> nameMap = new HashMap<String, List>();
	
	
	
    
	/**
	 * 添加列名字典，用于列名的命名
	 * @return
	 */
	public void addColumnName(String name, String displayName) {
		List<String> list = nameMap.get(name);
		if(list==null) {
			list = new ArrayList<String>();
			list.add(displayName);
			nameMap.put(name, list);			
		}else {
			if(list.contains(displayName)) {
				
			}else {
				list.add(displayName);
			}
		}
		
		
		
	}
	
	public static String getColumnName(String str) {
		return (String)(nameMap.get(str)).get(0);
	}
	

    public HashMap<String, DBTable> getTableMap() {
        return tableMap;
    }

    public void setTableMap(HashMap<String, DBTable> tableMap) {
        this.tableMap = tableMap;
    }
    
    public void put(String pmbName, DBTable pmbTable) {
	this.tableMap.put(pmbName.toUpperCase(), pmbTable);
    }
    
    public DBTable get(String pmbName) {
	return this.tableMap.get(pmbName.toUpperCase());
    }
    

    public String[] getTableNames() {	
	Object objects[] =  this.tableMap.keySet().toArray();
	String [] names = new String[objects.length];
	String [] sortNames = new String[objects.length];
	for(int i=0; i<objects.length; i++) {
	    names[i] =  (String)objects[i];
	}	
	
	long []arrays = new long[names.length];	
	for(int i=0; i<names.length; i++) {
		GridTable pmbTable = tableMap.get(names[i]);			
		arrays[i] = pmbTable.pmbNo;
	}
	
	
	Arrays.sort(arrays);
	for(int i=0; i<arrays.length; i++) {	
		String aName = getByPmbNo((int)arrays[i]);
		sortNames[i] = aName;		
	}
	
	return sortNames;
    }
    
    private String getByPmbNo(int pmbNo) {
    	Object objects[] =  this.tableMap.keySet().toArray();
    	String [] names = new String[objects.length];
    	String [] sortNames = new String[objects.length];
    	for(int i=0; i<objects.length; i++) {
    	    names[i] =  (String)objects[i];
    	}	
    	for(int i=0; i<names.length; i++) {
    		GridTable pmbTable = tableMap.get(names[i]);			
    		if(pmbTable.pmbNo == pmbNo) {
    			return pmbTable.getName();
    		}
    	}
		return null;
    }

    

    public List<GridTable> getList() {
    	String []anames = getTableNames();
    	list = new ArrayList<GridTable>();
    	for(int i=0; i<anames.length; i++) {
    		GridTable pmbTable = tableMap.get(anames[i]);	
    		list.add(pmbTable);
    	}
		return list;
    	
    }

    public FormTable getForm(String pmbName) {
    	return this.formMap.get(pmbName);
    }
    
    public void putForm(String pmbName, FormTable pmbTable) {
    	this.formMap.put(pmbName, pmbTable);
    }
    
    /**
     * SQL表
     * @param pmbName
     * @return
     */
    public SQLTable getSQL(String pmbName) {
    	return this.sqlMap.get(pmbName);
    }
    
    public void putSQL(String pmbName, SQLTable pmbTable) {
    	this.sqlMap.put(pmbName, pmbTable);
    }
    
    
    
	public HashMap<String, FormTable> getFormMap() {
		return formMap;
	}

	public void setFormMap(HashMap<String, FormTable> formMap) {
		this.formMap = formMap;
	}

	public static HashMap<String, GridSearchTable> getGridMap() {
		return gridMap;
	}

	public static void setGridMap(HashMap<String, GridSearchTable> gridMap) {
		AllTable.gridMap = gridMap;
	}
    
	  public GridSearchTable getGrid(String pmbName) {
	    	return this.gridMap.get(pmbName);
	    }
	    
	    public void putGrid(String pmbName, GridSearchTable pmbTable) {
	    	this.gridMap.put(pmbName, pmbTable);
	    }

		public HashMap<String, TableDataTable> getDataMap() {
			return dataMap;
		}

		public void setDataMap(HashMap<String, TableDataTable> dataMap) {
			this.dataMap = dataMap;
		}
	    
		public void putExcelTable(String sheetName,ExcelTable pmbTable) {
			HashMap<String, ExcelTable> aMap = excelMap.get(sheetName);
			if(aMap == null) {
				aMap = new HashMap<String, ExcelTable>();
				excelMap.put(sheetName, aMap);
			}			
			aMap.put(sheetName, pmbTable);
	    }
		
		public ExcelTable getExcelTable(String sheetName, String tableName) {
			HashMap<String, ExcelTable> aMap = excelMap.get(sheetName);
			if(aMap == null) {
				aMap = new HashMap<String, ExcelTable>();
			}	
			return aMap.get(tableName);
		}
		
		public List<ExcelTable> getExcelSheetTables(String sheetName) {
			List<ExcelTable> list = new ArrayList<ExcelTable>();
			HashMap<String, ExcelTable> aMap = excelMap.get(sheetName);
			if(aMap == null) {
				aMap = new HashMap<String, ExcelTable>();
			}	
			Iterator iter = aMap.values().iterator();
			while(iter.hasNext()) {
				ExcelTable aExcelTable = (ExcelTable) iter.next();
				list.add(aExcelTable);
			}
			return list;
		}
		
		public List<ExcelTable> getExcelTables() {
			Iterator sheetNames = excelMap.keySet().iterator();						
			List<ExcelTable> list = new ArrayList<ExcelTable>();
			while(sheetNames.hasNext()) {
				List<ExcelTable> subList = getExcelSheetTables(sheetNames.next().toString());
				list.addAll(subList);
			}			
			return list;
		}
		
		
    
}
